package com.example.librarymanagementsystem.service;

import com.example.librarymanagementsystem.mapper.BookMapper;
import com.example.librarymanagementsystem.model.BookInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : niu niu
 * @version : 1.0.0
 * @date : 2023/5/10 13:47
 */
@Service
public class BookService {
    @Autowired
    private BookMapper bookMapper;

    /**
     * 查询所有书籍
     * @return
     */
    public List<BookInfo> selectAll( BookInfo bookInfo){
        return bookMapper.selectAll(bookInfo);
    }

    /**
     * 查询所有数据条数
     * @return
     */
    public Integer countNum(){
        return bookMapper.countNum();
    }
    /**
     * 增加书籍
     * @param bookInfo
     * @return
     */
   public Integer insertBook(BookInfo bookInfo){
       if (bookInfo==null || !StringUtils.hasLength(bookInfo.getTitle())) {
           return -2;
       }
        return bookMapper.insertBook(bookInfo);
    }

    /**
     * 修改书籍
     * @param bookInfo
     * @return
     */
   public Integer updateBook(BookInfo bookInfo){
       if (bookInfo==null || ! StringUtils.hasLength(bookInfo.getTitle())) {
           return -2;
       }
        return  bookMapper.updateBook(bookInfo);
    }

    /**
     * 下架书籍
     * @param bookInfo
     * @param
     * @return
     */
   public Integer deleteBook(BookInfo bookInfo){
       System.out.println(bookInfo.getId());
       if (bookInfo.getId()==null || bookInfo.getId()==0) {
           return -2;
       }
        return bookMapper.deleteBook(bookInfo);
    }

    /**
     * 批量删除
     *
     */
   public Integer deleteBookByList(List<BookInfo> bookInfos){
       List<Integer> ids = new ArrayList<>();
       for (int i = 0; i < bookInfos.size(); i++) {
           ids.add(bookInfos.get(i).getId());
       }
       return bookMapper.deleteBookByList(ids);
    }
}
